package com.czl.module_service.fragment.warehousing

import androidx.databinding.DataBindingUtil
import com.czl.base.base.BaseFragment
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.FragmentWarehousingHomeAdapter
import com.czl.module_service.databinding.FragmentWarehousingHomeBinding
import com.czl.module_service.databinding.HeaderWarehousingHomeBinding
import com.czl.module_service.view.ListSheetDialogFragment
import com.czl.module_service.viewmodel.WarehousingHomeViewModel

/**
 * @author by xubin,
 * @date on 2022/1/18.
 * PS: Not easy to write code, please indicate.
 */
class WarehousingHomeFragment :
    BaseFragment<FragmentWarehousingHomeBinding, WarehousingHomeViewModel>() {

    private lateinit var mAdapter: FragmentWarehousingHomeAdapter

    var homeBinding: HeaderWarehousingHomeBinding? = null

    override fun initContentView(): Int {
        return R.layout.fragment_warehousing_home
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        activity?.window?.setBackgroundDrawableResource(R.color.color_F4F4F4)
        super.initData()
        viewModel.tvTitle.set("物品入库")
        initAdapter()
    }

    private fun initAdapter() {
        mAdapter = FragmentWarehousingHomeAdapter().apply {
            val view = HeaderWarehousingHomeBinding.inflate(layoutInflater)
            homeBinding = DataBindingUtil.bind(view.root)
            homeBinding?.setVariable(initVariableId(), viewModel)
            addHeaderView(homeBinding?.root!!)
        }

        binding.recyclerView.run {
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.bottomSheetTypeEvent.observe(this, { data ->
            val list = mutableListOf("其他入库", "采购入库", "工程入库")
            ListSheetDialogFragment.showBottomSheet(
                parentFragmentManager,
                list,
                block = {
                    viewModel.selectedType.set(list[it])
                }
            )
        })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        homeBinding?.unbind()
    }
}